/*************************************************************************
 *
 * $Id: hw.h 117 2007-01-22 15:49:41Z Ales $
 * 
 *************************************************************************/         

#ifndef _HW_H
#define _HW_H

/*****************
 * beep
 *****************/ 
#define Beep(x)             { if (x) { DDRD |= _BV(PD7); SetLed(LED_RED); } \
                              else { DDRD &= ~_BV(PD7); ClearLed(LED_RED); } }

/*****************
 * button
 *****************/ 
#define InitButtonHw()      { PORTB |= _BV(PB5) | _BV(PB6); \
                              PORTC |= _BV(PC0) | _BV(PC1); }

#define ReadKey()           ( ((~PINB) & (KEY_SET | KEY_FN)) | ((~PINC) & (KEY_MINUS | KEY_PLUS)) )

#define KEY_FN              _BV(PB6)
#define KEY_SET             _BV(PB5)
#define KEY_PLUS            _BV(PC1)
#define KEY_MINUS           _BV(PC0)

/*****************
 * serial
 *****************/ 
#define SerialSwitchTx()    { PORTD |= _BV(PD2); SetLed(LED_RED); }
#define SerialSwitchRx()    { PORTD &= ~_BV(PD2); ClearLed(LED_RED); }
#define InitSerialHw()      { DDRD |= _BV(PD2); }


///////////////////////////////////////////////////////////////////////////
#if (DISPLAY == 1602)

/*****************
 * backlight 
 *****************/ 
#define InitBacklightHw()   { DDRA |= _BV(PA6); }
#define BackLight(x)        { if (x) PORTA &= ~_BV(PA6); else PORTA |= _BV(PA6); }

/*****************
 * led
 *****************/
#define InitLedHw()         { DDRA |= _BV(PA0) | _BV(PA1) | _BV(PA2); }
#define SetLed(x)           { PORTA |= ((x) & (LED_GREEN | LED_YELLOW | LED_RED)); }
#define ClearLed(x)         { PORTA &= ~((x) & (LED_GREEN | LED_YELLOW | LED_RED)); }
//#define NegLed(x)           { PORTA ^= ((x) & (LED_GREEN | LED_YELLOW | LED_RED)); }

#define LED_GREEN           _BV(PA0)
#define LED_YELLOW          _BV(PA1)
#define LED_RED             _BV(PA2)

/*****************
 * lcd
 *****************/ 
#define LCD_PORT         PORTB        /**< port for the LCD lines   */
#define LCD_DATA0_PORT   LCD_PORT     /**< port for 4bit data bit 0 */
#define LCD_DATA1_PORT   LCD_PORT     /**< port for 4bit data bit 1 */
#define LCD_DATA2_PORT   LCD_PORT     /**< port for 4bit data bit 2 */
#define LCD_DATA3_PORT   LCD_PORT     /**< port for 4bit data bit 3 */
#define LCD_DATA0_PIN    0            /**< pin for 4bit data bit 0  */
#define LCD_DATA1_PIN    1            /**< pin for 4bit data bit 1  */
#define LCD_DATA2_PIN    2            /**< pin for 4bit data bit 2  */
#define LCD_DATA3_PIN    3            /**< pin for 4bit data bit 3  */
#define LCD_RS_PORT      PORTA        /**< port for RS line         */
#define LCD_RS_PIN       5            /**< pin  for RS line         */
#define LCD_RW_PORT      PORTA        /**< port for RW line         */
#define LCD_RW_PIN       4            /**< pin  for RW line         */
#define LCD_E_PORT       PORTA        /**< port for Enable line     */
#define LCD_E_PIN        3            /**< pin  for Enable line     */

///////////////////////////////////////////////////////////////////////////
#elif (DISPLAY == 2004)

/*****************
 * backlight 
 *****************/ 
#define InitBacklightHw()   { DDRA |= _BV(PA0); }
#define BackLight(x)        { if (x) PORTA &= ~_BV(PA0); else PORTA |= _BV(PA0); }

/*****************
 * led
 *****************/
#define InitLedHw()         { DDRA |= _BV(PA5) | _BV(PA6) | _BV(PA7); }
#define SetLed(x)           { PORTA |= ((x) & (LED_GREEN | LED_YELLOW | LED_RED)); }
#define ClearLed(x)         { PORTA &= ~((x) & (LED_GREEN | LED_YELLOW | LED_RED)); }

#define LED_GREEN           _BV(PA5)
#define LED_YELLOW          _BV(PA6)
#define LED_RED             _BV(PA7)

/*****************
 * lcd
 *****************/ 
#define LCD_PORT         PORTB        /**< port for the LCD lines   */
#define LCD_DATA0_PORT   LCD_PORT     /**< port for 4bit data bit 0 */
#define LCD_DATA1_PORT   LCD_PORT     /**< port for 4bit data bit 1 */
#define LCD_DATA2_PORT   LCD_PORT     /**< port for 4bit data bit 2 */
#define LCD_DATA3_PORT   LCD_PORT     /**< port for 4bit data bit 3 */
#define LCD_DATA0_PIN    0            /**< pin for 4bit data bit 0  */
#define LCD_DATA1_PIN    1            /**< pin for 4bit data bit 1  */
#define LCD_DATA2_PIN    2            /**< pin for 4bit data bit 2  */
#define LCD_DATA3_PIN    3            /**< pin for 4bit data bit 3  */
#define LCD_RS_PORT      PORTA        /**< port for RS line         */
#define LCD_RS_PIN       3            /**< pin  for RS line         */
#define LCD_RW_PORT      PORTA        /**< port for RW line         */
#define LCD_RW_PIN       2            /**< pin  for RW line         */
#define LCD_E_PORT       PORTA        /**< port for Enable line     */
#define LCD_E_PIN        1            /**< pin  for Enable line     */

///////////////////////////////////////////////////////////////////////////
#else
#error "Neznamy typ displeje DISPLAY v makefile"
#endif


#endif
